package edu.nf.controller;


import edu.nf.beans.annotations.Inject;
import edu.nf.dto.ResultDto;
import edu.nf.entity.Departm;
import edu.nf.exception.QueryException;
import edu.nf.exception.UpdateException;
import edu.nf.service.AddDepartmService;
import edu.nf.service.FindAllDepartmService;
import edu.nf.utils.UUIDUtil;
import edu.nf.web.ViewResult;
import edu.nf.web.annotation.RequestMapping;
import edu.nf.web.view.JsonView;

public class DepartmController {
    @Inject(beanName = "findAllDepartmService")
    private FindAllDepartmService findAllDepartmService;

    @Inject(beanName = "addDepartmService")
    private AddDepartmService addDepartmService;




    @RequestMapping(urlPattern = "/findAllDepartm")
    public ViewResult findAllDepartm(){
        ResultDto dto = new ResultDto();
        try{
            dto.setData(findAllDepartmService.findAllDepartm());
        }catch (QueryException e){
            dto.setCode(500);
            dto.setMessage(e.getMessage());
        }
        return new JsonView(dto);
    }

    @RequestMapping(urlPattern = "/addDepartm")
    public ViewResult addDepartm(String name){
        ResultDto dto = new ResultDto();
        try{
            Departm departm = new Departm();
            departm.setDepartmId(UUIDUtil.getUUID());
            departm.setName(name);
            addDepartmService.addDepartm(departm);

        }catch (UpdateException e){
            dto.setCode(500);
            dto.setMessage(e.getMessage());
        }
        return new JsonView(dto);
    }

}
